import { myPost } from '@/util/request'
import http from '@/util/request'
import { API_SERVER } from '../service'
import { flowStore } from '@/store/flow'

const SERVER =
  import.meta.env.VITE_REQUESTURL === '/test'
    ? import.meta.env.VITE_REQUESTURL
    : window.location.origin
const DEFAULT_API = `${API_SERVER}/v2`

/**
 * @desc 编辑工作流
 */
 export const editFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/edit`, data )
}

/**
 * @desc 新增工作流
 */
 export const createAIFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/add`, data )
}
/**
 * @desc 删除工作流
 */
 export const deleteFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/delete`, data )
}

/**
 * @desc 工作流配置详情
 */
 export const detailFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/detail`, data )
}

/**
 * @desc 工作流初始化
 */
 export const initFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/init/add`, data )
}

/**
 * @desc 工作流初始化详情
 */
 export const initFlowDetail = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/init/detail`, data )
}

/**
 * @desc 工作流发布
 */
 export const publishFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/publish`, data )
}

/**
 * @desc 查询所有可添加节点的列表
 */
 export const allNode = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/findall`, data )
}

/**
 * @desc 编辑节点
 * @param data 节点保存内容
 */
 export const editNode = (data) => {
  // 记录保存时间
  flowStore().setSaveTime(Date.now())
  return myPost(`${DEFAULT_API}/flow/node/edit`, data)
}

/**
 * @desc 新增节点
 */
 export const createNode = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/add`, data )
}

/**
 * @desc 重命名节点
 * @param data 节点重命名
 */
 export const renameNode = (data) => {
  // 记录保存时间
  flowStore().setSaveTime(Date.now())
  return myPost(`${DEFAULT_API}/flow/node/rename`, data )
}

/**
 * @desc 删除工作流节点
 */
 export const deleteNode = (data) => {
  // 记录保存时间
  flowStore().setSaveTime(Date.now())
  return myPost(`${DEFAULT_API}/flow/node/delete`, data )
}

/**
 * @desc 新增/删除节点连线
 */
 export const editEdges = (data) => {
  flowStore().setSaveTime(Date.now())
  return myPost(`${DEFAULT_API}/flow/node/selection`, data )
}
/**
 * @desc 编辑节点位置
 */
 export const editPosition = (data) => {
  flowStore().setSaveTime(Date.now())
  return myPost(`${DEFAULT_API}/flow/node/editxy`, data )
}

/**
 * @desc 查询插件列表
 */
 export const listPlugin = (data) => {
  return myPost(`${DEFAULT_API}/plugin/info/findall`, data )
}

/**
 * @desc 查询数据库列表
 */
 export const listDatabase = (data) => {
  return myPost(`${DEFAULT_API}/database/findall`, data )
}
/**
 * @desc 新增数据库
 */
 export const addDatabase = (data) => {
  return myPost(`${DEFAULT_API}/database/save`, data )
}

/**
 * @desc 查询数据库列表
 */
 export const listTable = (data) => {
  return myPost(`${DEFAULT_API}/flow/find/table`, data )
}
/**
 * @desc 查询数据库字段
 */
 export const listTableField = (data) => {
  return myPost(`${DEFAULT_API}/flow/find/field`, data )
}
/**
 * @desc 查询已发布的工作流列表
 */
 export const listPublishFlow = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/publishstatus`, data )
}
/**
 * @desc 查询已启用插件
 */
 export const listUseablePlugin = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/plugin`, data )
}

/**
 * @desc 附件上传
 */
 export const flowUploadURL = `${SERVER}${DEFAULT_API}/flow/node/upload`;

 export const flowUpload = (data) => {
  const formData = new FormData()
  for(const key in data) {
    formData.append(key, data[key])
  }

  return http({
    url: `${DEFAULT_API}/flow/node/upload`,
    method: 'post',
    headers: {
      'content-type': 'multipart/form-data'
    },
    timeout: 1500000,
    data: formData
  })
}

export const dicCode = (data) => {
  return myPost(`${DEFAULT_API}/flow/node/find/dic/code`, data )
}